import java.util.Arrays;

public class MyBitSet {
    public byte[] elem;
    public int useSize;

    /*
    无参构造方法
     */
    public MyBitSet() {
        this.elem = new byte[1];
    }

    /*
    带参数的构造方法
    size: 用户自行设置的大小，单位：元素个数
     */
    public MyBitSet(int size) throws IndexOutOfBoundException {
        if(size <= 0) {
            throw new IndexOutOfBoundException("大小设置不合理");
        }
        int num = size / 8 + 1;
        this.elem = new byte[num];
    }

    /*
    扩容方法
     */
    private void resize(int num) {
        elem = Arrays.copyOf(elem,num * 2);
    }

    public boolean set(int val) {
        int index = val / 8;
        if(index > elem.length) {
            resize(index);
        }
        elem[index] |= (1 << (val % 8));
        useSize++;
        return true;
    }

    public boolean get(int val) {
        int index = val / 8;

        int ret = elem[index] & (1 << (val % 8));
        if(ret == 0) {
            return false;
        }

        return true;
    }

    public int size() {
        return useSize;
    }
}
